package com.example.cognitivesystemserver.service;

import com.example.cognitivesystemserver.Model.UserModel;
import com.example.cognitivesystemserver.mapper.LoginMapper;
import com.example.cognitivesystemserver.mapper.StudentMapper;
import com.example.cognitivesystemserver.mapper.TeacherMapper;
import com.example.cognitivesystemserver.pojo.Admin;
import com.example.cognitivesystemserver.pojo.Login;
import com.example.cognitivesystemserver.pojo.Student;
import com.example.cognitivesystemserver.pojo.Teacher;
import com.example.cognitivesystemserver.utils.AssertUtil;
import com.example.cognitivesystemserver.utils.Base64Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public UserModel register(String name,String userId,String userPwd,int role) {

        AssertUtil.isTrue(loginMapper.queryLoginById(userId)!=null,"用户已存在");

        if(role==2){
            Teacher teacher=new Teacher();
            teacher.setName(name);
            teacher.setTeacherId(userId);
            teacherMapper.addTeacher(teacher);
        }

        if(role==3){
            Student student=new Student();
            student.setName(name);
            student.setStuId(userId);
            studentMapper.addStudent(student);
        }
        Login login=new Login();
        login.setUserid(userId);
        login.setPasswd(userPwd);
        login.setRole(role);
        loginMapper.insert(login);

        return buildUserInfo(login);

    }

    @Override
    public UserModel login(String userId,String userPwd) {
        checkLoginParams(userId,userPwd);

        Login login= loginMapper.queryLoginById(userId);

        AssertUtil.isTrue(login.getUserid()==null,"用户姓名不存在");

        //没有做密码校验
        AssertUtil.isTrue(login.getPasswd()==null,"用户密码不存在");

        //构建需要返回的用户对象
        return buildUserInfo(login);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updatePassWord(String userId, String oldPwd, String newPwd, String repeatPwd) {

        Login login =loginMapper.selectByUserId(userId);
        AssertUtil.isTrue(null==login,"待更新记录不存在");
        //参数校验
        checkPasswordParams(login,oldPwd,newPwd,repeatPwd);
        login.setPasswd(newPwd);
        AssertUtil.isTrue(loginMapper.updateByPrimaryKeySelective(login)<1,"修改密码失败");


    }

    private UserModel buildUserInfo(Login login){
        UserModel userModel=new UserModel();

        userModel.setUserIdStr(Base64Util.encoderUserID(login.getUserid()));
        //对象的名字

        int role= login.getRole();
        if(role==1){
            userModel.setUserName("管理员");
        }

        if(role==2){
            userModel.setUserName("老师");
        }

        if(role==3){
            userModel.setUserName("学生");
        }
        userModel.setUserName(login.getUserid());
        return userModel;
    }

    private void checkLoginParams(String userId, String userPwd) {
        //验证用户id
        AssertUtil.isTrue(StringUtils.isBlank(userId),"用户姓名不能为空！");

        //验证用户密码
        AssertUtil.isTrue(StringUtils.isBlank(userPwd),"用户密码不能为空！");

    }
    private void checkPasswordParams(Login login,String oldPwd,String newPwd,String repeatPwd){
        AssertUtil.isTrue(StringUtils.isBlank(oldPwd),"原始密码不能为空");
        AssertUtil.isTrue(!login.getPasswd().equals(oldPwd),"原始密码不正确");

        AssertUtil.isTrue(StringUtils.isBlank(newPwd),"新密码不能为空！");
        AssertUtil.isTrue(oldPwd.equals(newPwd),"新密码不能与原始密码相同！");

        AssertUtil.isTrue(StringUtils.isBlank(repeatPwd),"确认密码不能为空");
        AssertUtil.isTrue(!newPwd.equals(repeatPwd),"新密码与新密码不一致！");


    }

    public Login selectByUserId(String userId)throws DataAccessException{
        return loginMapper.selectByUserId(userId);
    }

}
